'use strict';

var _html = document.getElementsByTagName("html")[0];
_html.style.fontSize = document.documentElement.clientWidth / 750 * 100 + "px";
window.onresize = function(){
    _html.style.fontSize = document.documentElement.clientWidth / 750 * 100 + "px";
};

var URL;
var localUrl;

URL = 'http://192.168.1.50:9091';

// if((/(192\.168\.1\.)|(localhost)/).test(window.location.href)){
//     URL = 'http://192.168.1.50:9091';
//     //URL = 'http://192.168.1.216:9096';
// }else{
//     URL = 'https://appiwin.anjintech.com/'
//     // localUrl = 'http://appiwin.anjintech.com/'
// }

var vm = new Vue({
    el: '.wrap',
    data: {
        userPhone:'',
        userCode:'',
        userId:'',
        message: '',
        loading:false,
        codeflag:false,
        countdown:0
    },
    methods: {
        toast: function (msg) {
            if (!msg)return;
            $('#toast-message').remove();
            var str = $('<div id="toast-message">' + msg + '</div>');
            str.appendTo($('body'));
            str.stop().fadeOut(3000);
        },
        requestFun: function (url,params) {
            var _this=this
            _this.loading=false
            var _params = '';
            params && (_params = '?encryptData=' + encodeURIComponent(JSON.stringify({
                data: params
            })));
            var _url = URL + url + _params;
            var deferred = $.Deferred();
            $.post(_url, '', function(data) {
                _this.loading=false
                deferred.resolve(data.data);
            }, 'json');

            return deferred.promise();
        },
        href: function(link,params){
            if(link){
                var p = '';
                if(params){
                    p = '?';
                    for(var i in params){
                        if(p != '?')p += '&';
                        p += i + '=' + params[i];
                    }
                }
                window.location.href = link + p;
            }
        },
        changeTitle: function (title) {
            var $body = $('body');
            document.title = title;
            var $iframe = $('<iframe src="//m.baidu.com/favicon.ico"></iframe>').on('load', function () {
                setTimeout(function () {
                    $iframe.off('load').remove()
                }, 0)
            }).appendTo($body);
        },
        searchKey: function(key){
            var reg = new RegExp(key + '=[^&]+');
            var url = decodeURI(window.location.href);
            if((reg).test(url)){
                return url.match(reg)[0].split('=')[1];
            }
        },
        getcode:function () {  
            var _this=this
            if (_this.codeflag) {
                return false
            }
            console.log('获取验证码')
            if (!_this.userPhone) {
                _this.toast('请输入手机号')
                return  false
            }
            _this.codeflag=true
            _this.timerFun()
            var params={};
            params.userPhone= _this.userPhone
            // params.userPhone= '18282668833'      
            _this.requestFun('/financial/h5/getMessageCodeH5New', params).then(function(data) {
                console.log(data)
                if (data.resultCode=='0000') {
                     _this.toast("发送成功")
                    //  _this.userCode=data.resultData.code
                     console.log(data.resultData.code,"验证码")
                } else {
                 _this.toast(data.resultMessage)
                }
             });

        },
        timerFun:function () { 
            var _this=this
            _this.countdown=60
            var timer = setInterval(function() {
                _this.countdown-- 
                if (_this.countdown <= 0 ) {
                    _this.codeflag= false;  
                    _this.countdown="重新获取"      
                    clearInterval(timer);
                }
            }, 1000)
         }
    },
    computed: {
        type: function () {
            return this.searchKey('type');
        },
        isPartner: function () {
            return this.searchKey('isPartner');
        },
        weixinlink: function () {
            return this.searchKey('weixinlink');
        },
        inviterID: function () {
            return this.searchKey('inviterID');
        },
        userIdIp: function () {
            return this.searchKey('userId');
        },
    },
    created: typeof created != 'undefined' ? created : null
});